#!flask/bin/python
# -*- coding: utf8 -*-


import unittest
from unittest import TestCase
from app.models import Employee, EmployeeEducation, Job, JobType, Department, ScienceDomain, Course
from app import db, app
from config import basedir
import os

class TestSuite(unittest.TestCase):
	def setUp(self):
		print u'Setting up...'
		app.config['CSRF_ENABLED'] = False
		app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
		db.create_all()

	def tearDown(self):
		print 'Tearing down...'
		db.session.remove()
		db.drop_all()

	def test_sanity(self):
		assert(1 == 1)

	def test_coursecreation(self):		
		d = Department(departmenttitle='katedra1')
		d.sciencedomains.append(ScienceDomain(sciencedomaintitle='noblast1'))
		d.sciencedomains.append(ScienceDomain(sciencedomaintitle='noblast2'))
		
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet1'))
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet2'))
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet3'))
		
		d.sciencedomains[1].courses.append(Course(coursetitle='noblast2predmet1'))
		d.sciencedomains[1].courses.append(Course(coursetitle='noblast2predmet2'))

		db.session.add(d)
		db.session.commit()
		
		d = Department.query.filter(Department.departmenttitle=='katedra1').first()
		assert(d)
		
		sciencedomains = d.sciencedomains
		assert(len(sciencedomains) == 2)
		assert(sciencedomains[0].sciencedomaintitle == 'noblast1')
		assert(sciencedomains[1].sciencedomaintitle == 'noblast2')
		
		assert(len(sciencedomains[0].courses) == 3)
		assert(sciencedomains[0].courses[0].coursetitle == 'noblast1predmet1')
		assert(sciencedomains[0].courses[1].coursetitle == 'noblast1predmet2')
		assert(sciencedomains[0].courses[2].coursetitle == 'noblast1predmet3')
		
		assert(len(sciencedomains[1].courses) == 2)
		assert(sciencedomains[1].courses[0].coursetitle == 'noblast2predmet1')
		assert(sciencedomains[1].courses[1].coursetitle == 'noblast2predmet2')
		
	def test_coursedeletion(self):
		d = Department(departmenttitle='katedra1')
		d.sciencedomains.append(ScienceDomain(sciencedomaintitle='noblast1'))
		d.sciencedomains.append(ScienceDomain(sciencedomaintitle='noblast2'))
		
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet1'))
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet2'))
		d.sciencedomains[0].courses.append(Course(coursetitle='noblast1predmet3'))
		
		d.sciencedomains[1].courses.append(Course(coursetitle='noblast2predmet1'))
		d.sciencedomains[1].courses.append(Course(coursetitle='noblast2predmet2'))

		db.session.add(d)
		db.session.commit()
		
		no1 = d.sciencedomains[0]
		db.session.delete(no1)
		db.session.commit()
		
		d = Department.query.filter(Department.departmenttitle=='katedra1').first()
		assert(d)

		assert(len(d.sciencedomains) == 1)
		
		#Now, make sure that courses aren't deleted
		assert(len(Course.query.all()) == 5)
		
		# Make sure that courses from deleted sciencedomain have None as its sciencecomainid field
		assert(Course.query.filter(Course.coursetitle == 'noblast1predmet1').first().sciencedomainid == None)
		assert(Course.query.filter(Course.coursetitle == 'noblast1predmet2').first().sciencedomainid == None)
		assert(Course.query.filter(Course.coursetitle == 'noblast1predmet3').first().sciencedomainid == None)
		

if __name__ == u'__main__':
	unittest.main()
